package com.schoolET.service.admin.adminImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.entity.GoodsType;
import com.schoolET.entity.dto.GoodsTypeInfoDTO;
import com.schoolET.error.costom.NoGoodsTypeException;
import com.schoolET.mapper.GoodsTypeMapper;
import com.schoolET.service.admin.IGoodsTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品类型 服务实现类
 * </p>
 *
 * @author lin
 * @since 2024-06-04
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType> implements IGoodsTypeService {

    @Override
    public Page<GoodsTypeInfoDTO> inquireGoodsType(Integer pageNum, Integer pageSize) {
        //查询所有商品类型
        Page<GoodsType> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectGoodsType(page);
    }

    @Override
    public boolean addGoodsType(String type) {
        QueryWrapper<GoodsType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        GoodsType goodsType = baseMapper.selectOne(queryWrapper);
        if (goodsType != null) {
            throw new RuntimeException("该类型已存在");
        }
        GoodsType goodsType1 = new GoodsType();
        goodsType1.setType(type);
        baseMapper.insert(goodsType1);
        return true;
    }

    @Override
    public GoodsTypeInfoDTO getGoodsTypeInfo(Integer id) {
        GoodsType goodsType = baseMapper.selectById(id);
        if (goodsType == null) {
            throw new NoGoodsTypeException();
        }
        GoodsTypeInfoDTO goodsTypeInfoDTO = new GoodsTypeInfoDTO();
        BeanUtil.copyProperties(goodsType, goodsTypeInfoDTO);
        return goodsTypeInfoDTO;
    }

    @Override
    public List<GoodsTypeInfoDTO> queryAllGoodsType() {
        return baseMapper.selectAllGoodsType();
    }
}
